﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Drawing;
using IQKS.AWP.BusinessAccessLayer;
using System.Data;
using IQKS.AWP.DataAccessLayer;

public partial class Login : System.Web.UI.Page
{
    MembershipUser user;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            question.Visible = false;
            Answare.Visible = false;
            btnsubmit.Visible = false;
            if (Request.QueryString.Count == 0)
            {
                trSuccessMsg.Visible = false;
                trChangePassword1.Visible = false;
                trChangePassword2.Visible = false;
                trChangePassword3.Visible = false;
                cmdChangePassword.Visible = false;
            }
            else
            {
                try
                {
                    string[] strURLParts = Request.Url.OriginalString.Split('?');
                    string strUserEmail = EncryptDecrypt.Decrypt(strURLParts[1]);
                    string strUserName = Membership.GetUserNameByEmail(strUserEmail);
                    if (strUserName != null)
                        user = Membership.GetUser(strUserName);
                    if (user != null && user != null)
                    {
                        trSuccessMsg.Visible = false;
                        trEmailInfo.Visible = false;
                        cmdsubmit.Visible = false;
                        btnemail.Visible = false;
                        ViewState["EmailID"] = EncryptDecrypt.Encrypt("Email:" + strUserName);
                    }
                    else
                    {
                        UI_SetErrorText("Your account has not yet Registered with this email.");
                    }
                }
                catch (Exception Ex)
                {
                    UI_SetErrorText("Invalid link for the password reset.");
                }
            }
        }
    }


    protected void cmdsubmit_Click(object sender, EventArgs e)
    {
        string newPassword;
        if (Page.IsValid)
        {
            user = Membership.GetUser(Membership.GetUserNameByEmail(txtEmailID.Text.Trim()));
            try
            {
                newPassword = user.ResetPassword(txtanswer.Text.Trim());
                //user.IsLockedOut = false;
                //Membership.UpdateUser(user);
            }
            catch (MembershipPasswordException ex)
            {
                ErrorMsg.Visible = true;
                ErrorMsg.Text = "Invalid password answer. Please re-enter and try again.";
                return;
            }
            catch (Exception ex)
            {
                ErrorMsg.Visible = true;
                ErrorMsg.Text = ex.Message;
                return;
            }

            if (newPassword != null)
            {
                ErrorMsg.Visible = true;

                UserMasterBAL obj = new UserMasterBAL();
                string result = obj.GetPasswordByEmail(txtEmailID.Text.Trim(), newPassword);

                if (result == "SUCCESS")
                {
                    ErrorMsg.Text = "Password reset. Your new password is send to your Email Address";
                }
                else
                {
                    ErrorMsg.Text = "Password reset failed. Please re-enter your values and try again.";
                }
            }
            else
            {
                ErrorMsg.Visible = true;
                ErrorMsg.Text = "Password reset failed. Please re-enter your values and try again.";
            }
        }
    }
    protected void btnemail_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            string str = Membership.GetUserNameByEmail(txtEmailID.Text.Trim());

            if (str != null)
            {

                trEmailInfo.Visible = false;
                btnsubmit.Visible = false;
                cmdChangePassword.Visible = false;
                trSuccessMsg.Visible = true;
                btnemail.Visible = false;
                tdSuccessMsg.InnerText = "Password reset link email has been sent to your email address.";

                string strURL = Request.Url.AbsoluteUri + "?" + EncryptDecrypt.Encrypt(txtEmailID.Text.Trim());
                //"http://www.iqkonsumerstrategiez.com/webapp/common/ForgotPassword.aspx?" + EncryptDecrypt.Encrypt(txtEmailID.Text.Trim());
                string strEmail = "<a href='" + strURL + "'>Click here</a>";

                DataSet objDs;

                try
                {
                    objDs = DataUtils.ExecuteDataset("Usp_ResetPasswordLink", new object[] { txtEmailID.Text.Trim(), strEmail, "GetResetPassword" });

                }
                catch (Exception exception)
                {
                    throw new ApplicationException(exception.Message);
                }
            }
            else
            {
                ErrorMsg.Visible = true;
                ErrorMsg.Text = "Your account has not yet Registered with this email.";
            }
        }
    }
    protected void cmdChangePassword_Click(object sender, EventArgs e)
    {
        string strEmail = ViewState["EmailID"].ToString();
        string strUserEmail = EncryptDecrypt.Decrypt(strEmail).Replace("Email:", "");
        string strUserName = Membership.GetUserNameByEmail(strUserEmail);

        try
        {
            string strResult = Convert.ToString(DataUtils.ExecuteScalar("Usp_ResetPasswordLink", new object[] { strUserEmail, "", "ResetPassword" }));
            if (strResult == "Success")
            {
                bool blnResult = Membership.GetUser(strUserName).ChangePassword("Unilever@369", txtPassword.Text);

                trChangePassword1.Visible = false;
                trChangePassword2.Visible = false;
                trChangePassword3.Visible = false;
                cmdChangePassword.Visible = false;
                trSuccessMsg.Visible = true;
                tdSuccessMsg.InnerText = "Your password changed successfully.";
            }
            else
            {
                UI_SetErrorText(strResult);
            }

        }
        catch (Exception exception)
        {
            throw new ApplicationException(exception.Message);
        }

    }

    protected void UI_SetErrorText(string pStrText)
    {
        trSuccessMsg.Visible = false;
        trChangePassword1.Visible = false;
        trChangePassword2.Visible = false;
        trChangePassword3.Visible = false;
        cmdChangePassword.Visible = false;
        trEmailInfo.Visible = false;
        cmdsubmit.Visible = false;
        btnemail.Visible = false;


        ErrorMsg.Visible = true;
        ErrorMsg.Text = pStrText;
    }
}

